package com.three.crm.page.common;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.three.crm.R;
import com.three.crm.base.BaseActivity;
import com.three.crm.bean.MessageBean;
import com.three.crm.constant.AppConstant;
import com.three.crm.http.CommonResultCallBack;
import com.three.crm.require.AppRequireService;
import com.three.crm.util.AppManager;
import com.three.crm.util.SignUtil;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 消息详情
 * created by lqd on 2024-01-30.
 */
public class MessageActivity extends BaseActivity {

    private TextView tvTitle,tvContent,tvTime,tvSubTitle;
    private MessageBean bean;

    @Override
    protected void initParms(Bundle bundle) {
        if (bundle != null) {
            bean = bundle.getParcelable(AppConstant.EXTRAS_DATA);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_message;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText("消息详情");
        tvContent = $(R.id.tv_content);
        tvTime = $(R.id.tv_time);
        tvSubTitle = $(R.id.tv_subtitle);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
        }
    }

    @Override
    protected void initData(Context mContext) {
        if (bean != null) {
            tvContent.setText(bean.getContent());
            tvTime.setText(bean.getCtime());
            tvSubTitle.setText(bean.getTitle());
            if (bean.getIs_read() == 0) {
                onReadMessage(bean.getId());
            }
        }
    }

    private void onReadMessage(int id) {
        showLoading(getString(R.string.loading));
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userToken", AppManager.getToken());
            jsonObject.put("message_id",id);
            jsonObject.put("timestamp",getTimestamp());
            String sign = SignUtil.genSign(jsonObject);
            jsonObject.put("sign",sign);
            AppRequireService.readMessage(this, AppConstant.QUERY_DETAIL, jsonObject, new CommonResultCallBack(this) {
                @Override
                public void onResult(String result, int requireCode) {
                    hideLoading();
                    Log.i("TAG","result="+result);
                    if (requireCode == AppConstant.QUERY_DETAIL) {
                        AppManager.setNeedUpdate(true);
                    }
                }

                @Override
                public void onConnectTimeOut(Call call, Exception e, int requireCode) {
                    hideLoading();
                    showToast(e.getMessage());
                }

                @Override
                public void onFail(String msg, int requireCode) {
                    hideLoading();
                    showToast(msg);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}